package cn.supermaster.management.web.rest.system;

import cn.supermaster.management.common.annotation.Log;
import cn.supermaster.management.common.domain.SystemDepartment;
import cn.supermaster.management.common.domain.SystemRole;
import cn.supermaster.management.common.entity.Ztree;
import cn.supermaster.management.common.enums.BusinessType;
import cn.supermaster.management.common.response.WebResponse;
import cn.supermaster.management.web.service.system.ISDepartmentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * All rights Reserved, Designed By www.super-bytecode.com
 *
 * @ProjectName: supermaster-management
 * @Package: cn.supermaster.management.web.rest.system
 * @ClassName: SystemDepartmentRestController
 * @Description: []
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2021/2/19 22:01
 * @Version: V1.0
 * @Copyright: 2021 www.super-bytecode.com Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@RestController
@RequestMapping("/system/dept")
public class SystemDepartmentRestController {

    @Autowired
    private ISDepartmentService departmentService;

    @RequiresPermissions("system:dept:list")
    @PostMapping("/list")
    public List<SystemDepartment> queryDepartmentList(SystemDepartment dept) {
        return departmentService.queryDepartmentList(dept);
    }

    /**
     * 新增保存部门
     */
    @Log(title = "部门管理", businessType = BusinessType.INSERT)
    @RequiresPermissions("system:dept:add")
    @PostMapping("/add")
    public WebResponse addSave(@Validated SystemDepartment dept) {
        return departmentService.addDepartment(dept);
    }

    /**
     * 保存
     */
    @Log(title = "部门管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:dept:edit")
    @PostMapping("/edit")
    @ResponseBody
    public WebResponse editSave(@Validated SystemDepartment dept) {
        return departmentService.modifyDepartment(dept);
    }

    /**
     * 删除
     */
    @Log(title = "部门管理", businessType = BusinessType.DELETE)
    @RequiresPermissions("system:dept:remove")
    @GetMapping("/remove/{deptId}")
    @ResponseBody
    public WebResponse remove(@PathVariable("deptId") Long deptId) {
        return departmentService.deleteDepartment(deptId);
    }


    /**
     * 校验部门名称
     */
    @PostMapping("/checkDeptNameUnique")
    @ResponseBody
    public String checkDepartmentNameUnique(SystemDepartment dept) {
        return departmentService.checkDepartmentNameUnique(dept);
    }

    /**
     * 加载部门列表树
     */
    @GetMapping("/treeData")
    public List<Ztree> loadAllDepartmentTree() {
        return departmentService.loadAllDepartmentTree();
    }

    /**
     * 加载部门列表树（排除下级）
     */
    @GetMapping("/treeData/{excludeId}")
    public List<Ztree> treeDataExcludeChild(@PathVariable(value = "excludeId", required = false) Long excludeId) {
        return departmentService.loadExcludeChildDepartmentTree(excludeId);
    }

    /**
     * 加载角色部门（数据权限）列表树
     */
    @GetMapping("/roleDeptTreeData")
    @ResponseBody
    public List<Ztree> deptTreeData(SystemRole role) {
        return departmentService.loadRoleDepartmentTree(role);
    }
}